package com.rongshu.view.submitArticle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.widget.RelativeLayout;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.utils.DensityUtil;
import com.widget.qrcode.barcodescanner.BarcodeResult;
import com.widget.qrcode.barcodescanner.CaptureManager;
import com.widget.qrcode.barcodescanner.DecoratedBarcodeView;
import com.widget.titlebar.ImmersionBar;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/8.
 * description：扫描页面
 * <p>
 * 通过setResult(RESULT_OK, intent);方法将扫描结果返回给调用方
 */
public class SweepQRCodeActivity extends BaseActivity implements CaptureManager.ResultCallBack {
    @BindView(R.id.dbv_custom)
    DecoratedBarcodeView dbvCustom;
    @BindView(R.id.head_content)
    RelativeLayout headContent;
    private CaptureManager captureManager;
    private Bundle savedInstanceState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hintTitleBar();
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) headContent.getLayoutParams();
        layoutParams.topMargin = DensityUtil.getStateBar(this);
        headContent.setLayoutParams(layoutParams);
        this.savedInstanceState = savedInstanceState;
        startSweep();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_sweep_qrcode;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (captureManager != null)
            captureManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (captureManager != null) {
            captureManager.onResume();
            startSweep();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (captureManager != null)
            captureManager.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        if (captureManager != null)
            captureManager.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return dbvCustom.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    private void startSweep() {
        if (captureManager == null) {
            captureManager = new CaptureManager(this, dbvCustom);
            captureManager.initializeFromIntent(getIntent(), savedInstanceState);
        }
        captureManager.decode();
        captureManager.setResultCallBack(this);
    }

    @Override
    public void ZXingCallBackResult(BarcodeResult result) {
        toast(result.getText());
        Intent intent = new Intent();
        intent.putExtra(Constant.SWEEP_QRCODE_RESULT, result.getText());
        setResult(RESULT_OK, intent);
        finish();
    }

    @OnClick(R.id.head_left)
    public void onViewClicked() {
        finish();
    }
}
